package main

import "math/rand"

// Definition for singly-linked list.
// 定义节点
type ListNode struct {
	Val  int
	Next *ListNode
}

type Solution struct {
	Nodes *[]int
}

func Constructor(head *ListNode) Solution {
	// 转slice
	arr := make([]int, 0)
	for node := head; node != nil; node = node.Next {
		arr = append(arr, node.Val)
	}
	return Solution{&arr}
}

func (s *Solution) GetRandom() int {
	// 使用随机函数
	return (*s.Nodes)[rand.Intn(len(*s.Nodes))]
}

/**
 * Your Solution object will be instantiated and called as such:
 * obj := Constructor(head);
 * param_1 := obj.GetRandom();
 */
